package com.ssb.securedrestapp.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.ssb.securedrestapp.model.Account;
import com.ssb.securedrestapp.model.User;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

	@Autowired
	JdbcTemplate jdbcTemplate;

	private static final AtomicLong counter = new AtomicLong();

	private static List<User> users;

	static {
		users = populateDummyUsers();
	}

	@Override
	public List<User> findAllUsers() {
		return users;
	}

	@Override
	public User findById(long id) {
		for (User user : users) {
			if (user.getId() == id) {
				return user;
			}
		}
		return null;
	}

	@Override
	public User findByName(String name) {
		for (User user : users) {
			if (user.getName().equalsIgnoreCase(name)) {
				return user;
			}
		}
		return null;
	}

	@Override
	public void saveUser(User user) {
		user.setId(counter.incrementAndGet());
		users.add(user);
	}

	@Override
	public void updateUser(User user) {
		int index = users.indexOf(user);
		users.set(index, user);
	}

	@Override
	public void deleteUserById(long id) {

		for (Iterator<User> iterator = users.iterator(); iterator.hasNext();) {
			User user = iterator.next();
			if (user.getId() == id) {
				iterator.remove();
			}
		}
	}

	@Override
	public boolean isUserExist(User user) {
		return findByName(user.getName()) != null;
	}

	@Override
	public void deleteAllUsers() {
		users.clear();
	}

	private static List<User> populateDummyUsers() {
		List<User> users = new ArrayList<User>();
		users.add(new User(counter.incrementAndGet(), "Sam", 30, 70000));
		users.add(new User(counter.incrementAndGet(), "Tom", 40, 50000));
		users.add(new User(counter.incrementAndGet(), "Jerome", 45, 30000));
		users.add(new User(counter.incrementAndGet(), "Silvia", 50, 40000));
		return users;
	}

	@Override
	public void createAdminAccount(Account account) {
		String user = account.getUser();
		String pass = new BCryptPasswordEncoder().encode("ssb").toString();

		String a = "INSERT INTO `users` (`username`, `PASSWORD`, `enabled`) VALUES (?, ?, 1)";
		String b = "INSERT INTO `user_roles` (`username`, `role`) VALUES (?, 'ROLE_ADMIN')";
		String c = "INSERT INTO `user_roles` (`username`, `role`) VALUES (?, 'ROLE_USER')";
		jdbcTemplate.update(a, new Object[] { user, pass });
		jdbcTemplate.update(b, new Object[] { user });
		jdbcTemplate.update(c, new Object[] { user });
	}

}
